﻿using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace DataViewer
{
    public class FormShowData : Form
    {
        private ListView listView1;
        private ColumnHeader columnHeader1;
        private ColumnHeader columnHeader2;
        private readonly Config config;

        public FormShowData(Config config)
        {
            this.config = config;
            InitializeComponent();
            LoadCsvFiles();
        }

        private void InitializeComponent()
        {
            this.listView1 = new System.Windows.Forms.ListView();
            this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.SuspendLayout();
            // 
            // listView1
            // 
            this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.columnHeader1,
            this.columnHeader2});
            this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.listView1.FullRowSelect = true;
            this.listView1.GridLines = true;
            this.listView1.Location = new System.Drawing.Point(0, 0);
            this.listView1.Name = "listView1";
            this.listView1.Size = new System.Drawing.Size(800, 450);
            this.listView1.TabIndex = 0;
            this.listView1.UseCompatibleStateImageBehavior = false;
            this.listView1.View = System.Windows.Forms.View.Details;
            this.listView1.Font = new System.Drawing.Font("Microsoft YaHei", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.listView1.ItemActivate += new System.EventHandler(this.listView1_ItemActivate);
            // 
            // columnHeader1
            // 
            this.columnHeader1.Text = "文件名";
            this.columnHeader1.Width = 200;
            // 
            // columnHeader2
            // 
            this.columnHeader2.Text = "文件预览";
            this.columnHeader2.Width = 600;
            // 
            // FormShowData
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.listView1);
            this.Name = "FormShowData";
            this.Text = "数据查看器";
            this.ResumeLayout(false);
        }

        private void LoadCsvFiles()
        {
            try
            {
                var files = Directory.GetFiles(config.CsvFolder, "*.csv");
                foreach (var file in files)
                {
                    string fileName = Path.GetFileNameWithoutExtension(file); // 移除扩展名
                    string[] lines = File.ReadAllLines(file);
                    string preview = string.Join(Environment.NewLine, lines.Take(3)); // 取前3行
                    listView1.Items.Add(new ListViewItem(new[] { fileName, preview }));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载文件时出错：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void listView1_ItemActivate(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                string fileName = listView1.SelectedItems[0].Text + ".csv"; // 添加.csv后缀以获取完整文件名
                using (var detailForm = new FormDataDetail(config, fileName))
                {
                    detailForm.ShowDialog();
                }
            }
        }
    }
}
